package com.erp.erp_ui.Message;

import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.Message.IMessageService;
import com.erp.erp_servers.Message.IMessageUserDetailService;
import com.erp.erp_ui.Message.service.MessageAllService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zjk
 * @version 1.0
 * @description
 * @date 2021-11-23 20:26
 */
@Controller
@RequestMapping("messageUserDetail")
public class MessageUserDetailControl extends Base {
    private static final Logger logger = LoggerFactory.getLogger(MessageUserDetailControl.class);
    private final IMessageService messageService;
    private final IMessageUserDetailService messageUserDetailService;
    private final MessageAllService messageAllService;

    public MessageUserDetailControl(IMessageService messageService, IMessageUserDetailService messageUserDetailService, MessageAllService messageAllService) {
        this.messageService = messageService;
        this.messageUserDetailService = messageUserDetailService;
        this.messageAllService = messageAllService;
    }

    /**
     * 当前登录人所有接收消息分页查询
     * @return
     */
    @RequestMapping(value = "selectUserMessageByPage")
    @ResponseBody
    public R  selectUserMessageByPage(Integer type){
        try{
            UserEntity user = (UserEntity) this.getBean("user");
            PageInfo pageInfo = messageAllService.selectUserMessageByPage(type,user.getCode());
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 接收人自己的消息 批量删除
     * @return
     */
    @RequestMapping(value = "delUerMessage",method = RequestMethod.POST)
    @ResponseBody
    public R delUerMessage(String[] ids){
        try{
            List<Integer> list=new ArrayList<>();
            for(int i=0;i<ids.length;i++){
                list.add(Integer.parseInt(ids[i].replace("[","").replace("]","")));
            }
            UserEntity user = (UserEntity) this.getBean("user");
            messageAllService.delUerMessage(list,user.getCode());
            return R.build(ResultStatusEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 批量已读 接收人自己的消息
     * @return
     */
    @RequestMapping(value = "readUerMessage",method = RequestMethod.POST)
    @ResponseBody
    public R readUerMessage(String[] ids){
        try{
            List<Integer> list=new ArrayList<>();
            for(int i=0;i<ids.length;i++){
                list.add(Integer.parseInt(ids[i].replace("[","").replace("]","")));
            }
            UserEntity user = (UserEntity) this.getBean("user");
            messageUserDetailService.updateMessageReadStatus(list,user.getCode());
            return R.build(ResultStatusEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }


}
